﻿using SimpleTerminal.Common;
using Microsoft.Win32;
using System;
using System.Windows.Forms;

namespace SimpleTerminal
{
    public partial class SelectPort : Form
    {
        private Common.SerialConnectionInfo serialConnectionInfo = new Common.SerialConnectionInfo();

        internal SerialConnectionInfo SerialConnectionInfo { get => serialConnectionInfo; set => serialConnectionInfo = value; }

        public SelectPort()
        {
            InitializeComponent();
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0219)
            {   //设备被拔出
                if (m.WParam.ToInt32() == 0x8000 || m.WParam.ToInt32() == 0x8004)//usb串口
                {
                    UpdatePortList();
                }
            }
            base.WndProc(ref m);
        }

        private void UpdatePortList()
        {
            RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
            if (keyCom != null)
            {
                string[] sSubKeys = keyCom.GetValueNames();
                this.portList.Items.Clear();
                foreach (string sName in sSubKeys)
                {
                    string sValue = (string)keyCom.GetValue(sName);
                    this.portList.Items.Add(sValue);
                }
                if (this.portList.Items.Count > 0)
                {
                    this.portList.Text = (string)portList.Items[0];
                }
            }
        }

        public void SetPortComList()
        {
            UpdatePortList();

            int i;
            //单个添加
            for (i = 300; i <= 38400; i = i * 2)
            {
                this.comboBox2.Items.Add(i.ToString());  //添加波特率列表
            }

            //批量添加波特率列表
            string[] baud = { "43000", "56000", "57600", "115200", "128000", "230400", "256000", "460800" };
            this.comboBox2.Items.AddRange(baud);




            //设置默认值
            portList.SelectedText = "COM1";
            comboBox2.SelectedText = "115200";
            comboBox3.SelectedText = "8";
            comboBox4.SelectedText = "None";
            comboBox5.SelectedText = "1";
        }
        private void SelectPort_Load(object sender, EventArgs e)
        {
            SetPortComList();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (portList.Text.Length == 0)
            {
                Windows.Error("请选择一个串口");
                return;
            }

            this.DialogResult = DialogResult.OK;
            serialConnectionInfo.PortName = portList.SelectedText;
            serialConnectionInfo.BaudRate = Convert.ToInt32(comboBox2.SelectedText);
            serialConnectionInfo.DateBits = Convert.ToInt32(comboBox3.SelectedText);
            serialConnectionInfo.Parity = comboBox4.SelectedText;
            serialConnectionInfo.StopBits = comboBox5.SelectedText;
            this.Close();
            //Form1 form1 = (Form1)this.Owner;
            //form1.openUartPort(portList.Text,comboBox2.Text,comboBox3.Text,comboBox4.Text,comboBox5.Text);
            //this.DialogResult = DialogResult.OK;
        }
    }
}
